/**
 * Copyright(c) 2014 Xunton Solutions
 *
 * History:
 *   15-12-12 下午1:55 Created by dwzhang
 */
package com.jade.bss.base.customer.impl;
 
import com.jade.bss.base.customer.CustomerException;
import com.jade.bss.base.customer.EnterpriseDao;
import com.jade.bss.base.customer.EnterpriseManager;
import com.jade.bss.base.customer.info.EnterpriseInfo;
import com.jade.framework.base.criterion.Criteria;
import com.jade.framework.base.criterion.Order;
import com.jade.framework.base.criterion.Restrictions;
import com.jade.framework.base.util.PagingList;
import org.apache.commons.lang3.StringUtils;
import org.springframework.transaction.annotation.Transactional;

import javax.ejb.TransactionAttribute;
import javax.inject.Inject;
import javax.inject.Named;
import java.util.List;

/**
 * 地址管理接口实现。
 *
 * @author <a href="mailto:dwzhang@xunton.com">dwzhang</a> 
 * @version 1.0 15-12-12 下午1:55
 */
@Named ("bss_enterpriseInfoManager")
public class EnterpriseManagerImpl implements EnterpriseManager
{
    private EnterpriseDao entityDao;

    @Inject
    public void setEnterpriseDao(@Named ("bss_enterpriseInfoDao") EnterpriseDao entityDao)
    {
        this.entityDao = entityDao;
    }

    @TransactionAttribute
    @Transactional (rollbackFor = Exception.class)
    public long add(EnterpriseInfo address)
            throws CustomerException
    {

        entityDao.add(address);
        return -1;
    }

    @TransactionAttribute
    @Transactional (rollbackFor = Exception.class)
    public void update(EnterpriseInfo address)
            throws CustomerException
    {
        EnterpriseInfo old = entityDao.get(address.getId());
        if (old == null) {
            throw new CustomerException("err.bss.customer.address_not_existed", null, address.getId());
        }

        entityDao.update(address);

    }

    @TransactionAttribute
    @Transactional (rollbackFor = Exception.class)
    public void remove(long... ids)
            throws CustomerException
    {
        for (long id : ids) {
            EnterpriseInfo address = entityDao.get(id);
            if (address != null) {
                entityDao.remove(address);
            }
        }
    }

    public EnterpriseInfo get(long id)
    {
        return entityDao.get(id);
    }

    public PagingList<EnterpriseInfo> query(Criteria criteria)
    {
        return entityDao.query(criteria);
    }
}
